import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
	mode: "history",
	routes: [
	{
		path: '/index',
		component: () => import('@/pages/Home'),
	},
	{
		path: '/user',
		component: () => import('@/pages/User'),
	},
	{
		path: '/message',
		component: () => import('@/pages/Message'),
	},
	{
		path: '/message/chat/:username',
		component: () => import('@/pages/Message/Chat'),
	},
	{
		path: '/authorization',
		component: () => import('@/pages/Auth'),
	},
	{
		path: '/service/:id',
		component: () => import('@/pages/Home/Service/ServiceItem'),
	},
	{
		path: '/user/edit',
		component: () => import('@/pages/User/EditUserInfo'),
	},
	{
		path: '/user/order',
		component: () => import('@/pages/User/Order'),
	},
	{
		path: '/user/address',
		component: () => import('@/pages/User/Address'),
	},
	{
		path: '/user/address/add',
		component: () => import('@/pages/User/Address/AddAddress'),
	},
	{
		path: '/user/address/edit',
		component: () => import('@/pages/User/Address/EditAddress'),
	},
	{
		path: '/user/order/payment/:id',
		component: () => import('@/pages/User/Order/PaymentOrder'),
	},
	{
		path: '/user/deal',
		component: () => import('@/pages/User/Deal'),
	},
	{
		path: '/doctor',
		component: () => import('@/pages/Home/Swipe/Doctor'),
	},



	// 重定向，每次访问*都定向到首页
	{
		path: '',
		redirect: '/index',
	}
	],
	// 控制路由跳转滚动条的位置为最顶部
	scrollBehavior(to, from, savedPosition) {
		// 始终滚动到顶部，y代表距离顶部的距离
		return {
			y: 0
		}
	},
})

router.beforeEach((to, from, next) => {
	// 没登陆的就拦截
	if (to.path.match("/userhome") || to.path.match("/teamhome")) {
		var token = localStorage.getItem("token")
		if(token == null){
			next("/index")
		}
	}
	next()
})


export default router